import UUID from './utils/uuid'
import * as Watermark from './utils/Watermark'
import * as WaterMark2PDF from './utils/WaterMark2PDF'
import PageForm from './PageForm.vue'
import PageCard from './PageCard.vue'
import PCard from './Card.vue'
import UpLoadFile from './UpLoadFie.vue'
import QueryCard from './QueryCard.vue'
import PageItemQueryCard from './PageItemQueryCard.vue'
import PreviewShow from './previewShow.vue'
import ImgGroup from './PageFormItem/ImgGroup.vue'
import AuditProcess from './AuditProcess.vue'
import Notification from './utils/notification'
import ruleWatermark from './utils/ruleWatermark'

import isType from './utils/is'

const addWatermark = async (url)=> {
  //['jpg', 'png', 'jpeg','pdf']
  const getFileSuffix = (filePath) => {
    if(!filePath){
      return ''
    }
    var startIndex = filePath.lastIndexOf(".");
    if (startIndex != -1)
      return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
    else return "";
  }

  const picFun = async (url) => {
    const tempBase = await Watermark.adddWatermark(url)
    const url_ = Watermark.base64Img2Blob(tempBase, window)
    return url_
  }
  const SuffixList = {
    'jpg': picFun,
    'png': picFun,
    'jpeg': picFun,
    "pdf": async (url) => {
      const str = localStorage.getItem('pro__Login_Userinfo')
      const userInfo = str ? JSON.parse(str) : {};
      // console.log('@@@@@@@@@@@@@@@@@@@@@', userInfo.value);
      let WatermarkText = '请勿外传'
      if (userInfo.value) {
        const { phone, realname } = userInfo.value
        if(phone){

          WatermarkText = `${realname} ${phone&&phone.substr(7, 4)}`
        }else{
          WatermarkText = realname

        }
      }
      const url_ = await WaterMark2PDF.addPDFWaterMark(url, { text: WatermarkText, size: 10 })
      return url_
    }
  }
  const res = { url: '', suffix: '' }
  const fileSuffix = getFileSuffix(url)
  res.suffix = fileSuffix
  if(!ruleWatermark(url)){
    res.url = url
    return res
  }
  if (SuffixList[fileSuffix]) {
    const url_ = await SuffixList[fileSuffix](url)
    res.url = url_
  } else {
    res.url = url
  }
  return res
}



export default {
    install(Vue) {
        Vue.prototype.UUID = UUID
        Vue.prototype.AddWatermark = {Watermark,WaterMark2PDF, addWatermark}
        Vue.prototype.Notification = Notification
        Vue.prototype.isType = isType
        Vue.component('PageForm', PageForm)
        Vue.component('PageCard', PageCard)
        Vue.component('PCard', PCard)
        Vue.component('UpLoadFile', UpLoadFile)
        Vue.component('QueryCard', QueryCard)
        Vue.component('PageItemQueryCard', PageItemQueryCard)
        Vue.component('PreviewShow', PreviewShow)
        Vue.component('ImgGroup', ImgGroup)
        Vue.component('AuditProcess', AuditProcess)

    }
}
